package com.symbio.ishenapi.background.controller;

import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.background.model.TmGroupMembers;
import com.symbio.ishenapi.background.vo.GroupMembersVO;
import com.symbio.ishenapi.background.vo.MembersVO;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description:分组成员管理控制层
 * @Date: Created in 17:37 2018/11/7
 * @Modified By:
 */
@RequestMapping("/tmGroupMembers")
public class TmGroupMembersController extends BaseController{

    /**
     * 获取组长列表
     */
    public void findLeaderList(){
        try {
            //后期维护字典获取成员code    暂时0.组长1.成员
            String roleCode = "0";
            String groupId = getPara("groupId");
            Map map = TmGroupMembers.dao.findListByParam(getQueryModel(),roleCode,groupId,null);
            renderPageJsonRet(map.get("count"),map.get("data"));
        }catch (Exception e){
            renderJsonError(50002,"组长列表查询");
        }
    }

    /**
     * 获取成员列表
     */
    public void findMembersList(){
        try {
            //后期维护字典获取成员code    暂时0.组长1.成员
            String roleCode = "1";
            String groupId = getPara("groupId");
            String userType = getPara("userType");
            Map map = TmGroupMembers.dao.findListByParam(getQueryModel(),roleCode,groupId,userType);
            renderPageJsonRet(map.get("count"),map.get("data"));
        }catch (Exception e){
            renderJsonError(50002,"成员列表查询");
        }
    }

    /**
     * 分组-组长添加
     */
    public void saveGroupLeader(){
        try {
            TmGroupMembers tmGroupMembers = (TmGroupMembers)getReqBodyObject(TmGroupMembers.class);
            TmGroupMembers.dao.saveGroupLeader(tmGroupMembers);
            renderJsonError(50001,"组长添加");
        }catch (Exception e){
            renderJsonError(50002,"组长添加");
        }
    }

    /**
     * 分组-成员添加
     */
    public void saveMembersList(){
        try {
            GroupMembersVO groupMembersVO = (GroupMembersVO)getReqBodyObject(GroupMembersVO.class);
            TmGroupMembers.dao.saveMembersList(groupMembersVO);
            renderJsonError(50001,"成员添加");
        }
        catch (NullPointerException e){
            renderJsonError(50002,"数据为空异常");
        }
        catch (Exception e){
            renderJsonError(50002,"成员添加");
        }
    }

    /** POST
     * @Author: chen
     * @Description:医生端群成员添加
     * @Date: 15:59 2018/12/7
     * @param: [
     *              {
     *               "groupId":"999",       ---群id
     *               "userIds":"1,2,3",     ---群成员ids集合
     *               "userType":"1",        ---0.医生1.患者
     *               "userReferees":"999"   ---引荐人
     *               }
     *          ]
     * @return: void
     */
    public void saveMemberListToDoctor(){
        try {
            MembersVO membersVO = (MembersVO)getReqBodyObject(MembersVO.class);
            TmGroupMembers.dao.saveMemberListToDoctor(membersVO);
            renderJsonError(50001,"成员添加");
        }catch (Exception e){
            e.printStackTrace();
            renderJsonError(50002,"成员添加");
        }
    }

    /**
     * chen
     * 分组成员删除
     * id 组成员id
     */
    public void delete(){
        try {
            String id = getPara("id");
            TmGroupMembers.dao.deleteMember(Integer.parseInt(id));
            renderJsonError(50001,"成员删除");
        }catch (Exception e){
            renderJsonError(50002,"成员删除");
        }
    }

    /** POST
     * @Author: chen
     * @Description:医生端成员分组创建
     * @Date: 11:21 2018/11/26
     * @param: [
     *          {
                    "departmentId":"14",        ---科室id
                    "groupName":"文杰2组",       ---组名称
                    "userType":"0",             ---组成员类型 0.医生 1.患者
                    "members":[
                    {
                    "doctorId":"999",           ---医生id
                    "doctorName":"13 测试人员1",  ---医生姓名
                    "memberRoleCode":"180919016" ---医生类型
                    }
                    ]
                    }
     * ]
     * @return: void
     */
//   public void saveGroupMembers(){
//        try {
//            GroupMembersVO groupMembersVO = (GroupMembersVO)getReqBodyObject(GroupMembersVO.class);
//            int group = TmGroupInfo.dao.queryByNameAndDepId(groupMembersVO.getGroupName(),groupMembersVO.getDepartmentId());
//            if(group > 0){
//                renderJsonError(50003,"科组名称已存在");
//                return;
//            }
//            TmGroupMembers.dao.saveGroupMembers(groupMembersVO);
//            renderJsonError(50001,"分组成员创建");
//        }catch (Exception e){
//            renderJsonError(50002,"分组成员创建");
//        }
//   }

    /** POST
     * @Author: chen
     * @Description:医生端成员分组创建
     * @Date: 11:21 2018/11/26
     * @param: [
     *          {
    "departmentId":"14",        ---科室id
    "userType":"0",             ---组成员类型 0.医生 1.患者
    "members":[
    {
    "doctorId":"999",           ---医生id
    "doctorName":"13 测试人员1",  ---医生姓名
    "memberRoleCode":"180919016" ---医生类型
    }
    ]
    }
     * ]
     * @return: void
     */
     public void saveGroupMembers(){
         try {
             GroupMembersVO groupMembersVO = (GroupMembersVO)getReqBodyObject(GroupMembersVO.class);
             TmGroupMembers.dao.saveGroupMembers(groupMembersVO);
             renderJsonError(50001,"成员添加");
         }catch (Exception e){
             renderJsonError(50002,"成员添加");
         }
     }

     /** GET
      * @Author: chen
      * @Description:医生端科室分组获取患者列表
      * @Date: 13:36 2018/11/29
      * @param:
      * @return:
      */
     public void findPatientMembers(){
         try {
             //字典后期维护
             String userType = "患者";
             String groupId = getPara("groupId");
             String doctorId = getPara("doctorId");
             List<Record> data = TmGroupMembers.dao.findPatientMembers(groupId,userType,doctorId);
             renderJsonRet(data);
         }catch (Exception e){
             renderJsonError(50002,"科室分组患者查询");
         }
    }

    /** GET
     * @Author: chen
     * @Description:根据群id删除组内成员
     * @Date: 10:51 2018/12/4
     * @param: [
     *              groupId     ---群id
     *              userId      ---成员id
     *          ]
     * @return: void
     */
    public void deleteMembersByGroupId(){
        try {
            String groupId = getPara("groupId");
            String userIds = getPara("userIds");
            TmGroupMembers.dao.deleteMembersByGroupId(groupId,userIds);
            renderJsonError(50001,"成员删除");
        }catch (Exception e){
            e.printStackTrace();
            renderJsonError(50002,"成员删除");
        }
    }

    /** GET
     * @Author: chen
     * @Description:获取成员所在的组id集合
     * @Date: 12:04 2018/12/4
     * @param: [
     *              userId      ---成员id
     *          ]
     * @return: void
     */
    public void findMemberGroupIds(){
        try {
            String userId = getPara("userId");
            List<Integer> ids = TmGroupMembers.dao.findMemberGroupIds(userId);
            renderJsonRet(ids);
        }catch (Exception e){
            e.printStackTrace();
            renderJsonError(50002,"获取成员所在的组ids");
        }
    }
}
